'use strict';

angular.module('hanxinBroker').directive('checkIsNumber', function checkIsNumber() {
  return {
    restrict: 'A',
    require: 'ngModel',
    link: function (scope, element, attrs, ngModel) {
      ngModel.$parsers.push(function (value) {
        if (_.isUndefined(value)) {
          return undefined;
        }
        // 必须是一个数字
        var isNumber = !_.isNaN(value);
        ngModel.$setValidity('number', isNumber);
        var min = +attrs.min;
        var isMin = (_.isNaN(min) || value >= min);
        ngModel.$setValidity('min', isMin);
        var max = +attrs.max;
        var isMax = (_.isNaN(max) || value <= max);
        ngModel.$setValidity('max', isMax);
        return isNumber && isMin && isMax ? value : undefined;
      });
    }
  };
});
